package cn.ecnu.service.impl;

import cn.ecnu.common.constant.BusinessConstant;
import cn.ecnu.mapper.ConsultantSupervisorConnectorMapper;
import cn.ecnu.mapper.SupervisorMapper;
import cn.ecnu.mapper.UserMapper;
import cn.ecnu.pojo.ConsultantSupervisorConnector;
import cn.ecnu.pojo.Supervisor;
import cn.ecnu.pojo.User;
import cn.ecnu.service.SupervisorService;
import cn.ecnu.vo.fromFront.ChangeConsultantVo;
import cn.ecnu.vo.fromFront.ChangeDefaultDutyVo;
import cn.ecnu.vo.fromFront.SupervisorForm;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static cn.ecnu.common.constant.BusinessConstant.CONSULTANT_DEFAULT_DUTY_KEY;
import static cn.ecnu.common.constant.BusinessConstant.SUPERVISOR_DEFAULT_DUTY_KEY;

@Service
public class SupervisorServiceImpl implements SupervisorService {

    @Resource
    private ConsultantSupervisorConnectorMapper connectorMapper;

    @Resource
    private SupervisorMapper supervisorMapper;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @Resource
    private UserMapper userMapper;

    @Override
    public int addSupervisor(SupervisorForm vo) {
        try {
            User user = new User();
            user.setRole(2);
            BeanUtils.copyProperties(vo, user);
            userMapper.insertUser(user);
            Supervisor supervisor = new Supervisor();
            BeanUtils.copyProperties(vo, supervisor);
            supervisor.setSupervisorId(user.getUserId());
            supervisor.setCountTotal(0);
            supervisor.setTimeTotal(0);
            supervisorMapper.insertSupervisor(supervisor);
            ValueOperations<Object, Object> ops = redisTemplate.opsForValue();
            ops.set(String.format(BusinessConstant.SUPERVISOR_ONGOING_COUNT_KEY, user.getUserId()), 0);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }

    @Override
    public void changeDefaultDuty(ChangeDefaultDutyVo vo) {
        try {
            SetOperations<Object, Object> opsForSet = redisTemplate.opsForSet();
            for (int i = 1; i <= 7; i++) {
                if (BooleanUtils.isTrue(opsForSet.isMember(String.format(SUPERVISOR_DEFAULT_DUTY_KEY, i), vo.getUserId()))) {
                    opsForSet.remove(String.format(SUPERVISOR_DEFAULT_DUTY_KEY, i), vo.getUserId()); // 删除已有的排班
                }
            }
            vo.getDefaultDuty().forEach((day) -> {
                opsForSet.add(String.format(SUPERVISOR_DEFAULT_DUTY_KEY, day), vo.getUserId());
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void changeConsultant(ChangeConsultantVo vo) {
        try {
            connectorMapper.deleteAllBindingBySupervisorId(vo.getUserId());
            vo.getConsultants().forEach((id) -> {
                ConsultantSupervisorConnector connector = new ConsultantSupervisorConnector();
                connector.setConsultantId(id);
                connector.setSupervisorId(vo.getUserId());
                connectorMapper.insertConnector(connector);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
